﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 出入库记录
    /// </summary>
    [ApiDescriptionSettings("统计查询", Name = "View_AccessDetails", Order = 100)]
    [Route("api/[Controller]")]
    public class View_AccessDetailsService : IView_AccessDetailsService, IDynamicApiController, ITransient
    {
        private readonly IRepository<v_AccessDetails,MasterDbContextLocator> _view_AccessDetailsRep;


        public View_AccessDetailsService(
            IRepository<v_AccessDetails,MasterDbContextLocator> view_AccessDetailsRep
        )
        {
            _view_AccessDetailsRep = view_AccessDetailsRep;
        }

        /// <summary>
        /// 分页查询出入库记录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<PageResult<View_AccessDetailsOutput>> Page([FromQuery] View_AccessDetailsSearch input)
        {
            var view_AccessDetailss = await _view_AccessDetailsRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Taskno), u => EF.Functions.Like(u.TaskNo, $"%{input.Taskno.Trim()}%"))
                                     .Where(input.Taskmodel != null, u => u.TaskModel == input.Taskmodel)
                                     .Where(input.Tasktype != null, u => u.TaskType == input.Tasktype)
                                     .Where(input.Taskstatus != null, u => u.TaskStatus == input.Taskstatus)
                                     .Where( u => u.TaskNo != null)
                                     .Where(!string.IsNullOrEmpty(input.Sourceplace), u => EF.Functions.Like(u.SourcePlace, $"%{input.Sourceplace.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Toplace), u => EF.Functions.Like(u.ToPlace, $"%{input.Toplace.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Containercode), u => EF.Functions.Like(u.ContainerCode, $"%{input.Containercode.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialname), u => EF.Functions.Like(u.MaterialName, $"%{input.Materialname.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialno), u => EF.Functions.Like(u.MaterialNo, $"%{input.Materialno.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialbatch), u => EF.Functions.Like(u.MaterialBatch, $"%{input.Materialbatch.Trim()}%"))
                                     .Where(!string.IsNullOrEmpty(input.Materialspec), u => EF.Functions.Like(u.MaterialSpec, $"%{input.Materialspec.Trim()}%"))
                                     .OrderBy(PageInputOrder.OrderBuilder<View_AccessDetailsSearch>(input))
                                     .ProjectToType<View_AccessDetailsOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);
            return view_AccessDetailss;
        }

        

    }
}
